package com.chuji.crmcj.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.R;
import com.chuji.crmcj.act.ClientDetailAct;
import com.chuji.crmcj.act.PotentialListAct;
import com.chuji.crmcj.adapter.PotentialListAdapter;
import com.chuji.crmcj.bean.MagPossibleInfo;
import com.chuji.crmcj.bean.PotentialInfo;
import com.chuji.crmcj.bean.TaskRecordDetailsInfo;
import com.chuji.crmcj.utils.Jump;
import com.chuji.crmcj.utils.NumberUtils;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.util.ArrayList;
import java.util.List;

public class MagPossibleClientFragment extends Fragment {
    /*通知布局*/
    private ListView lv_possible_client;
    /*自定义适配器*/
    private CustomPossibleClientAdapter customPossibleClientAdapter;
    /*网络数据存储*/
    List<MagPossibleInfo.ApiParamObjEntity> magPossibleData;
    /*网络数据*/
    private MagPossibleInfo magPossibleInfo;
    //销售员ID
    String SalesID;
    //用户ID
    String UserID;
    Intent intent;
    @Override
    public void onStart() {
        super.onStart();
        reqDataBySale();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mag_possible_client, container, false);
        lv_possible_client= (ListView) view.findViewById(R.id.lv_possible_client);
        //获取ID
        SalesID=getActivity().getIntent().getStringExtra("SalesID");
        UserID=SPUtils.getString(UIUtils.getContext(),"UserID","");

        return view;
    }
    private void reqDataBySale() {
        if(magPossibleData==null){
            magPossibleData=new ArrayList<MagPossibleInfo.ApiParamObjEntity>();
        }
//        String strURL = String.format(UrlUtils.GETHIDDENLISt + "&SalesID=F248611C-CB39-4806-919E-71DCD085D208&CarModel=&CustomerLevel=&SeqType=0&PageSize=10&PageIndex=1");
        String strURL = String.format(UrlUtils.GET_POSSIBLE_CLIENT + "&UserID="+UserID+"&SalesID="+SalesID);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                magPossibleInfo = JSON.parseObject(response, MagPossibleInfo.class);
                magPossibleData.addAll(magPossibleInfo.getApiParamObj());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        // 判断adapter是否创建
                        if (customPossibleClientAdapter == null) {
                            //创建适配器
                            customPossibleClientAdapter=new CustomPossibleClientAdapter();
                            //填充数据
                            lv_possible_client.setAdapter(customPossibleClientAdapter);
                        } else {
                            customPossibleClientAdapter.notifyDataSetChanged();
                        }
                        //给ListView设置点击事件
                        lv_possible_client.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                intent=new Intent(UIUtils.getContext(), ClientDetailAct.class);
                                intent.putExtra("CustomerID",magPossibleData.get(position).getCustomerID());
                                startActivity(intent);
                            }
                        });
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }
    /**
     * 自定义适配器
     */
    class CustomPossibleClientAdapter extends BaseAdapter {
        private ViewHolder holder;
        @Override
        public int getCount() {
            return magPossibleData.size();
        }

        @Override
        public Object getItem(int i) {
            return magPossibleData.get(i);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            if (view == null) {
                holder = new ViewHolder();
                view = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.mag_listview_possible_client, null);
                holder.mTv_client_name = (TextView) view.findViewById(R.id.tv_client_name);
                holder.mTv_client_phoneNumber = (TextView) view.findViewById(R.id.tv_client_phoneNumber);
                holder.mTv_car_type = (TextView) view.findViewById(R.id.tv_car_type);
                holder.mTv_handle_time = (TextView) view.findViewById(R.id.tv_handle_time);
                view.setTag(holder);
            } else {
                    holder = (ViewHolder) view.getTag();
                }
            if(magPossibleData.size()!=0){
                holder.mTv_client_name.setText(magPossibleData.get(0).getName());
                if(magPossibleData.size()==11) {
                holder.mTv_client_phoneNumber.setText(NumberUtils.parsePhoneNumber(magPossibleData.get(0).getTel()));
                }else{
                    holder.mTv_client_phoneNumber.setText(magPossibleData.get(0).getTel());
                }
                holder.mTv_client_phoneNumber.setText(magPossibleData.get(0).getTel());
                holder.mTv_car_type.setText(magPossibleData.get(0).getCarModel());
                holder.mTv_handle_time.setText(magPossibleData.get(0).getLastTime());
            }
            return view;
        }
    }
    static class ViewHolder {
        private TextView mTv_client_name;
        private TextView mTv_client_phoneNumber;
        private TextView mTv_car_type;
        private TextView mTv_handle_time;

    }
}
